package com.cheetah.start.common.utils;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * @author xjhmwj
 * @date 2021/7/1 9:17
 */
@Slf4j
public class MyIdUtil {

    /**
     * 生成不重复编号 年月日时分秒毫秒纳秒 20210701172823511022 synchronized  Thread.sleep(1); 缺一不可，我多线程测试过不会有重复ID
     *
     * @return {@link String}
     */
    public synchronized static String orderNum() {
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        String yyyyMMddHHmmss = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        long l = System.currentTimeMillis();
        long l2 = System.nanoTime();
        String ms = l + "";
        String ns = l2 + "";
        return yyyyMMddHHmmss + ms.substring(ms.length() - 3) + ns.substring(ns.length() - 3);
    }
}
